<?php
// +----------------------------------------------------------------------
// | Etion [ http://www.etion.cn ]
// +----------------------------------------------------------------------
// | File: TagModel.class.php
// +----------------------------------------------------------------------
// | Date: 2010-5-7
// +----------------------------------------------------------------------
// | Author: 李汝康 <37739419@qq.com>
// +----------------------------------------------------------------------
// | 文件描述: acts 数据表模型
// +----------------------------------------------------------------------

class ActsModel extends Model {

	/**
	 * 添加入库之前自动调用
	 * @param unknown_type $data
	 * @param unknown_type $options
	 */
	protected function _before_insert(&$data,$options) {
		parent::_before_insert($data,$options);
		if (is_array($data['allow'])){
			$data['allow'] = implode(',',$data['allow']);
		}
		if (is_array($data['deny'])){
			$data['deny'] = implode(',',$data['deny']);
		}

	}
	/**
	 * 更新入库之前自动调用
	 * @param unknown_type $data
	 * @param unknown_type $options
	 */
	protected function _before_update(&$data,$options) {
		$this->_before_insert($data,$options);
	}
	/**
	 * find之后自动调用
	 * @param unknown_type $data
	 * @param unknown_type $options
	 */
	protected function _after_find(&$result,$options) {
		parent::_after_find($result,$options);
		if (empty($result['allow'])) $result['allow'] = array();
		else $result['allow'] = explode(',',$result['allow']);

		if (empty($result['deny'])) $result['deny'] = array();
		else $result['allow'] = explode(',',$result['allow']);
	}
	/**
	 * findAll or select 之后自动调用
	 * @param unknown_type $resultSet
	 * @param unknown_type $options
	 */
	protected function _after_select(&$resultSet,$options) {
		parent::_after_select($resultSet,$options);
		if (is_array ( $resultSet )) {
			foreach ( $resultSet as $k => $v ) {
				if (isset($resultSet [$k] ['allow'])) {
					if (empty($resultSet [$k] ['allow'])) $resultSet [$k] ['allow'] = array();
					else $resultSet [$k] ['allow'] = explode(',',$resultSet [$k] ['allow']);
				}
				if (isset($resultSet [$k] ['deny'])) {
					if (empty($resultSet [$k] ['deny'])) $resultSet [$k] ['deny'] = array();
					else $resultSet [$k] ['deny'] = explode(',',$resultSet [$k] ['deny']);
				}
			}
		}
	}

	/**
	 * ajax方式保存授权，单个
	 */
	public function ajaxSave($data) {
		if (empty($data['controller']) || empty($data['action'])
			|| empty($data['do']) || !$data['role_id'] || empty($data['appname'])) return null;
		//获取角色信息
		$role_data = D('Role')->find((int)$data['role_id']);
		if(is_array($role_data)) {
			$where = array(
				"appname" => $data['appname'],
				"controller" => $data['controller'],
				"action" => $data['action'],
			);
			$act_data = $this->where($where)->find();
			if (is_array($act_data)) {
				if ($data['do'] == 'add') {
					if (!in_array($role_data['name'],$act_data['allow'])) {
						array_push($act_data['allow'],$role_data['name']); //添加角色访问权限
					}
				} else if ($data['do'] == 'remove') {
					if (in_array($role_data['name'],$act_data['allow'])) {
						$key = array_search($role_data['name'],$act_data['allow']);
						unset($act_data['allow'][$key]);
					}
				}
				//print_r($act_data);
				return $this->save($act_data);
			}			
		}
		return false;
	}

	/**
	 * 检查控制器和其动作是否已经被注册
	 * @param array $data
	 */
	public function isActsExists($data) {
		if (!is_array($data) || empty($data['appname']) || empty($data['controller']) || empty($data['action'])) return false;
		$where = array(
			"appname" => $data['appname'],
			"controller" => $data['controller'],
			"action" => $data['action']
		);
		$act_data = $this->where($where)->find();
		if (is_array($act_data)) return true;
		else return false;
	}


	/**
	 * 缓存ACT文件
	 */
	public function cacheAct() {
		$group = $this->field(array("DISTINCT appname AS `appname`"))->findAll();
		foreach ($group as $g) {
			$where = array("appname"=>$g['appname']);
			$controller = $this->where($where)->group("controller")->findAll();
			if (is_array($controller)) {
				$data = array();
				foreach ($controller as $v) {	//控制器的所有已经注册动作
					$w = array(
						'controller' => $v['controller'],
						'appname' => $g['appname'],
					);
					$actions = $this->where($w)->findAll();
					$module = ucfirst(strtolower($v['controller']));
					$data[$module]['allow'] = 'RBAC_HAS_ROLE';
					$data[$module]['actions'] = array();					
					if (is_array($actions)) {
						foreach ($actions as $a) {
							$data[$module]['actions'][$a['action']]['allow'] = $a['allow'];
							$data[$module]['actions'][$a['action']]['deny'] = $a['deny'];
						}
					}
				}
			}			
			F ( $g['appname'] . '_act_cache', $data );
		}
		return true;
	}
}

?>